home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- #include "stdio.h"
- #include "ctype.h"
- #include "math.h"
- #include "malloc.h"
- #include "demograph.h"
-
- typedef struct {
- char title[MAXTITLE];
- float firstyear;
- float nvalues;
- float monthspan;
- float maxvalue;
- float minvalue;
- float *values;
- } FILEDATA;
-
- #define DBUG FALSE
-
- char *montharray[] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug",
- "Sep","Oct","Nov","Dec"};
-
-
- extern CATEGORY *categoryroot, *curcategory;
- extern usage[];
-
- /* GETDATA gets the data from the listed data files */
- getdata(argc, argv)
- int argc;
- char *argv[];
- {
- FILE *file;
- int i, lnum, fct;
-
- int readata(FILE *file, char *filename);
-
- FILE *openfile(char *arg, char *readwrite);
-
- if (argc < 2) {
- fprintf(stderr,"%s",usage);
- exit(1);
- }
- fct = 0;
- for (i = 1; i < argc; i++) {
- if (strcmp("-s",argv[i]) == 0) {
- i++;
- continue;
- }
- if (strcmp("-f",argv[i]) == 0) {
- continue;
- }
-
- file = openfile(argv[i],"r");
-
- if ((lnum = readdata(file, argv[i])) != 0)
- fprintf(stderr, "error in data file: %s line %d\n", argv[i], lnum);
- /* no error returned from categories, so do not need to clean up */
- else
- fct++;
- }
-
- /* fprintf (stderr,"rct=%d\n",fct); */
- if (fct == 0) {
- fprintf(stderr,"%s",usage);
- exit(1);
- }
-
- curcategory = categoryroot;
- }
-
-
- /* READDATA reads the information from a data file. The first non commented
- line in a file has to match the demographic magic number. The next non
- comment line must contain the title of the data. The next non commented
- line contains three integers: the first year of the data, number of data
- entrys per state, and the time between entrys in months. These three data
- items are separated by spaces. Each successive line following contains a
- data list for one of 49 states (including the District of Columbia) in
- alphabetical order. Comments are contained on a sinle line and the first
- character of a comment line is '#'.
- */
- int readdata(file, filename)
- FILE *file;
- char *filename;
- {
- FILEDATA filedata;
- char string[MAXSTRING];
- float *curvalue;
- int magic;
- int s, v;
- long lnum;
- char *strptr, *oldptr;
- char *chr;
-
- int readln(FILE *file, char *string, long *lnum);
- void make_lookup_table(FILEDATA filedata);
-
- /* get next non comment line */
- if (readln(file, string, &lnum) <= 0)
- return (lnum);
-
- /* check for magic number (to check file type) */
- if (sscanf(string, "%d", &magic) != 1 || magic != MAGIC) {
- fprintf(stderr, "file is not a demographic data file: %s\n", filename);
- return (lnum);
- }
-
- /* get next non comment line */
- if (readln(file, string, &lnum) <= 0)
- return (lnum);
-
- /* get data title (need to remove '\n') */
- if (strlen(strncpy(filedata.title, string, MAXTITLE)) >= MAXTITLE) {
- filedata.title[MAXTITLE - 1] = '\0';
- }
- chr=filedata.title;
- while (*chr != '\n' && *chr != '\0') {
- chr++;
- }
- if (*chr == '\n') {
- *chr = '\0';
- }
-
- /* get next non comment line */
- if (readln(file, string, &lnum) <= 0)
- return (lnum);
-
- /* get data parameters */
- if (sscanf(string, "%f %f %f", &filedata.firstyear, &filedata.nvalues,
- &filedata.monthspan) != 3) {
- return (lnum);
- }
-
- /* make room for data structure */
- if (DBUG) fprintf(stderr,"\nmallocing (%d*%d*%d)=%d bytes\n",
- (int)filedata.nvalues,NUMSTATES,sizeof(float),
- (int)filedata.nvalues * NUMSTATES * sizeof(float));
-
- if ((filedata.values = (float *) malloc((int)filedata.nvalues * NUMSTATES *
- sizeof(float))) == NULL) {
- fprintf(stderr, "error in memory allocation 1\n");
- exit(1);
- }
-
- /* init min and max data values */
- filedata.maxvalue = 0.0;
- filedata.minvalue = 4294967296.0; /* 2^^32 large number */
-
- /* get the data for each state */
- curvalue = filedata.values;
- for (s = 0; s < NUMSTATES; s++) {
-
- /* get next non comment line */
- if (readln(file, string, &lnum) <= 0) {
- free(filedata.values);
- filedata.values = NULL;
- return (lnum);
- }
- /* pointer to parse numbers in file */
- strptr = &string[0];
-
- for (v = 0; v < filedata.nvalues; v++) {
- /*
- * get next number in string. if not enough numbers return
- * error
- */
- oldptr = strptr;
- *curvalue = (float)strtod(strptr,&strptr);
- if (strptr == oldptr) { /* not enough data on line */
- free(filedata.values);
- filedata.values = NULL;
- return (lnum);
- }
-
- /* get the max and min values for scaling */
- if (*curvalue > filedata.maxvalue)
- filedata.maxvalue = *curvalue;
- if (*curvalue < filedata.minvalue)
- filedata.minvalue = *curvalue;
-
- if (DBUG) fprintf(stderr,"%.1f ",*curvalue);
-
- curvalue++;
-
- }
- if (DBUG) fprintf(stderr,"\n");
- }
-
- make_lookup_table(filedata);
- free(filedata.values);
- filedata.values = NULL;
-
- /* if no errors return 0 */
- return (0);
- }
-
-
- /* READLN gets the next line of non commented text.
- */
- int readln(file,string,lnct)
- FILE *file;
- char *string;
- long *lnct;
- {
- char c;
-
- do {
- (*lnct)++;
-
- /* check for end of file - return 0 */
- if (fgets(string, MAXSTRING, file) == NULL)
- return (0);
-
- /* check for error - return negative */
- if (sscanf(string, "%c", &c) == 0)
- return (-1);
-
- } while (c == '#' || c == '\n');
-
- /* all is well - return positive */
- return (1);
- }
-
-
- /* MAKE_LOOKUP_TABLE makes a lookup table of size MAXDATA (based on the screen
- size) associated with a demographic category (file). This table will be
- indexed based on the slider bar position. Each data entry contains a date
- and a list of values for each state associated with that date.
- */
- void make_lookup_table(filedata)
- FILEDATA filedata;
- {
- if (DBUG)
- fprintf(stderr, "\nmallocing %d bytes\n", sizeof(CATEGORY));
-
- /* allocate space for this file (category) */
- if (categoryroot == NULL) {
- if ((categoryroot = (CATEGORY *) malloc(sizeof(CATEGORY))) == NULL) {
- fprintf(stderr, "error in memory allocation 2\n");
- exit(1);
- }
- curcategory = categoryroot;
- } else {
- if ((curcategory->next=(CATEGORY *)malloc(sizeof(CATEGORY))) == NULL) {
- fprintf(stderr, "error in memory allocation\n");
- exit(1);
- } else {
- curcategory = curcategory->next;
- }
- }
-
- curcategory->next = NULL;
-
- /* length checked when we read in the title */
- strcpy(curcategory->title, filedata.title);
- if (DBUG)
- fprintf(stderr, "%s\n", curcategory->title);
-
- if (DBUG)
- fprintf(stderr, "%f\n", curcategory->scale);
-
- /*
- * expand data to fill in (presumably) larger data array for quick
- * index this takes more memory, but saves computation during display.
- * This will definitely be graphics (and memory) intensive
- */
- {
- register int iindex;
- register int d;
- register int i;
- register int dayinc =(int) filedata.nvalues;
- register float *valueptr;
- register float *today;
- register float *tomorrow;
- register DATA *dataptr = curcategory->dataarray;
-
- float year;
- float month;
- float findex;
- float firstyear = filedata.firstyear;
- float monthspan = filedata.monthspan / 12.0;
- float ndatarecs = MAXDATA - 1;
- float ndatavals = filedata.nvalues - 1;
- float offset = filedata.minvalue;
- float scale = 1.0 / (filedata.maxvalue - filedata.minvalue);
-
-
-
- #ifdef DBUG
- int datacount = 0;
- #endif
-
- /* fprintf(stderr,"min%f max%f o%f s%f \n",
- filedata.minvalue, filedata.maxvalue, offset, scale);
- */
- for (d = 0; d < MAXDATA; d++) {
-
- findex = d * ndatavals / ndatarecs;
- iindex = (int) findex;
-
- year = firstyear + findex * monthspan;
- findex -= iindex;
- month = findex * 12.0;
-
- sprintf(dataptr->date, "%s %d",montharray[(int)month],(int)year);
- if (DBUG)
- fprintf(stderr, "%s\n", dataptr->date);
-
- today = filedata.values + iindex;
- tomorrow = (iindex == ndatavals) ? today : today + 1;
- valueptr = dataptr->data;
-
- for (i = 0; i < NUMSTATES; i++) {
-
- *valueptr = *today + (*tomorrow - *today) * findex;
-
- /* normalize value */
- /* fprintf (stderr,"v1=%f o=%f s=%f ",
- *valueptr, offset, scale); */
- *valueptr = (*valueptr - offset) * scale;
- /* fprintf (stderr,"v2=%f\n",*valueptr); */
- valueptr++;
-
- if (DBUG) {
- fprintf(stderr," %.3f ",*today+(*tomorrow-*today)*findex);
- datacount++;
- if ((datacount % 16) == 0)
- fprintf(stderr, "\n");
- }
-
- today += dayinc;
- tomorrow += dayinc;
- }
-
- if (DBUG) {
- fprintf(stderr, "\n\n");
- datacount = 0;
- }
- dataptr++;
- }
- }
- }
-
-